# -*- coding: utf-8 -*-
from django.db import models

# -*- coding: utf-8 -*-
from django.db import models

# Create your models here.

from django.contrib import admin

## Other
from django.contrib.auth.models import User

import caching.base

######################################################################################################################
####用户
class WikiUser(caching.base.CachingMixin,models.Model):
    name = models.OneToOneField(User,verbose_name='用户',related_name="WikiUser")

    def __unicode__(self):
        return self.name.get_full_name()

    class Meta:
        verbose_name = 'Wiki用户'


class WikiUserAdmin(admin.ModelAdmin):
    search_fields = ['name']
    list_display = ['name']

admin.site.register(WikiUser,WikiUserAdmin)



######################################################################################################################
## 组
class WikiGroup(caching.base.CachingMixin,models.Model):
    name = models.CharField(max_length=30,verbose_name="组名")
    member = models.ManyToManyField(WikiUser,null=True,verbose_name="成员")



    def __unicode__(self):
        return self.name

    class Meta:
        verbose_name = 'Wiki用户组'


class WikiGroupAdmin(admin.ModelAdmin):
    search_fields = ['name']
    list_display = ['name']

admin.site.register(WikiGroup,WikiGroupAdmin)





######################################################################################################################
##  页面
class Page(caching.base.CachingMixin,models.Model):
    title = models.CharField(max_length=255,verbose_name="标题", unique=True)
    is_redirect = models.BooleanField(default=False, verbose_name="是否跳转")


    def __unicode__(self):
        return self.title
    class Meta:
        verbose_name = '页面'

    def get_latest_archive(self):
        try:
            return self.get_archive.order_by("-timestamp").all()[0]
        except:
            return None

class PageAdmin(admin.ModelAdmin):
    search_fields = ['title']
    list_display = ['title','is_redirect']
    # list_display = ['Name','ServiceArea']

admin.site.register(Page,PageAdmin)


######################################################################################################################
## 重定向
class Redirect(caching.base.CachingMixin,models.Model):
    title = models.CharField(max_length=255,verbose_name="标题")
    page_form = models.OneToOneField(Page,related_name="redirect",verbose_name="来源")
    page_to = models.OneToOneField(Page,related_name="come_form",verbose_name="目标")
    activation = models.BooleanField(default=True,verbose_name="有效的")


    def __unicode__(self):
        return self.title
    class Meta:
        verbose_name = '重定向'

class RedirectAdmin(admin.ModelAdmin):
    search_fields = ['title']
    list_display = ['title','page_form','page_to','activation']
    # list_display = ['Name','ServiceArea']

admin.site.register(Redirect,RedirectAdmin)


######################################################################################################################
#  分类
class Category(caching.base.CachingMixin,models.Model):
    title = models.CharField(max_length=255,verbose_name="标题")
    parent = models.ForeignKey("Category",null=True,blank=True,verbose_name="父级",related_name="get_sub_category")
    root = models.BooleanField(default=True,verbose_name="根级")

    def __unicode__(self):
        return self.title
    class Meta:
        verbose_name = '分类'



class CategoryAdmin(admin.ModelAdmin):
    search_fields = ['title']
    list_display = ['title','parent','root']
    # list_display = ['Name','ServiceArea']

admin.site.register(Category,CategoryAdmin)


######################################################################################################################
#  档案
class Archive(caching.base.CachingMixin,models.Model):
    page = models.ForeignKey(Page,related_name="get_archive",verbose_name="对应页面")
    title = models.CharField(max_length=255,verbose_name="标题", null=True, blank=True)
    content = models.TextField(null=True, blank=True,verbose_name="内容")
    timestamp = models.DateTimeField(auto_now_add=True,verbose_name="时间戳")
    editor = models.ForeignKey(WikiUser,verbose_name="修改者")
    editor_fullname = models.CharField(max_length=40,verbose_name="修改者姓名", null=True, blank=True)
    ip = models.CharField(max_length=255,null=True,blank=True,verbose_name='修改者IP')
    category = models.ForeignKey(Category,verbose_name="分类",related_name="get_archive")

    def __unicode__(self):
        return self.title
    class Meta:
        verbose_name = '档案'
        ordering = ["-timestamp"]


class ArchiveAdmin(admin.ModelAdmin):
    search_fields = ['title']
    list_display = ['page','title','timestamp']
    # list_display = ['Name','ServiceArea']

admin.site.register(Archive,ArchiveAdmin)

######################################################################################################################
## 近期的变化
class RecentChange(caching.base.CachingMixin,models.Model):

    page = models.ForeignKey(Page,related_name="get_recent_change",verbose_name="对应页面")

    title = models.CharField(max_length=255,verbose_name="标题", null=True, blank=True)
    timestamp = models.DateTimeField(auto_now_add=True,verbose_name="时间戳")
    editor = models.ForeignKey(WikiUser,verbose_name="修改者")
    editor_fullname = models.CharField(max_length=40,verbose_name="修改者姓名", null=True, blank=True)
    this_old = models.ForeignKey(Archive,verbose_name="修改前状态", related_name="before_change")
    this_last = models.ForeignKey(Archive,verbose_name="修改后状态", related_name="after_change")
    ip = models.CharField(max_length=255,null=True,blank=True,verbose_name='修改者IP')



    def __unicode__(self):
        return self.title
    class Meta:
        verbose_name = '近期的变化'

class RecentChangeAdmin(admin.ModelAdmin):
    search_fields = ['title']
    list_display = ['page','title','timestamp']
    # list_display = ['Name','ServiceArea']

admin.site.register(RecentChange,RecentChangeAdmin)

######################################################################################################################
## 标签
class Tag(caching.base.CachingMixin,models.Model):
    title = models.CharField(max_length=32,verbose_name="标题", null=True, blank=True)
    archives = models.ManyToManyField(Archive,related_name="get_tag",verbose_name="属于页面", null=True )

    def __unicode__(self):
        return self.title
    class Meta:
        verbose_name = '标签'

class TagAdmin(admin.ModelAdmin):
    search_fields = ['title']
    list_display = ['title']
    # list_display = ['Name','ServiceArea']

admin.site.register(Tag,TagAdmin)

######################################################################################################################
## 图片
class Picture(caching.base.CachingMixin,models.Model):
    title = models.CharField(max_length=32,verbose_name="标题", null=True, blank=True)
    file = models.ImageField(verbose_name="文件",upload_to="Wiki/Picture/")
    def __unicode__(self):
        return self.title
    class Meta:
        verbose_name = '图像'

class PictureAdmin(admin.ModelAdmin):
    pass
    # list_display = ['Name','ServiceArea']

admin.site.register(Picture,PictureAdmin)